﻿using System;
using Quartz;
using Spring.Scheduling.Quartz;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Net;
using Common.Logging;

namespace DotNetServiceLib.Core
{
    /// <summary>
    /// Task that fires an HTTP petition to a specific URL.
    /// </summary>
    public class TaskURL : QuartzJobObject, ITask
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private string name = "";
        private string target = "";
        private string arguments = "";
        private bool loggingIsEnabled = true;
        
        protected override void ExecuteInternal(JobExecutionContext context)
        {
            if (!String.IsNullOrEmpty(target))
            {
                var request = (HttpWebRequest)WebRequest.Create(target);
                request.Referer = target; // optional
                request.UserAgent =
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; " +
                    "Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; " +
                    ".NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; " +
                    "InfoPath.2; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)";
                try
                {
                    var response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        if (loggingIsEnabled)
                            log.Info(String.Format("HTTP petition to {0} returned OK 200.", target));
                    }
                    else {
                        log.Error(String.Format("URL return status was not OK 200 {0}", response.ToString()));
                    }
                    
                }
                catch (WebException webExc)
                {
                    log.Error(String.Format("An error occured while firing URL {0}: {1}", target, webExc.ToString()));
                }
            }
            else {
                log.Error("No URL was provided.");
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
            }
        }
        public string Arguments
        {
            get
            {
                return arguments;
            }
            set
            {
                arguments = value;
            }
        }
        public bool LoggingIsEnabled
        {
            get
            {
                return loggingIsEnabled;
            }
            set
            {
                loggingIsEnabled = value;
            }
        }

        #region Miembros de ITask


        public string ClassName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string MethodName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
